package com.google.code.sms.android.util;

import java.util.List;

import com.google.code.sms.android.activity.SMSTabActivity;
import com.google.code.sms.android.domain.MediaElement;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

/**
 * @author Sindre Mehus
 */
public class EntryAdapter extends ArrayAdapter<MediaElement.Entry> {

    private final SMSTabActivity activity;
    private final ImageLoader imageLoader;
    private final boolean checkable;

    public EntryAdapter(SMSTabActivity activity, ImageLoader imageLoader, List<MediaElement.Entry> entries, boolean checkable) {
        super(activity, android.R.layout.simple_list_item_1, entries);
        this.activity = activity;
        this.imageLoader = imageLoader;
        this.checkable = checkable;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MediaElement.Entry entry = getItem(position);

        if (entry.isDirectory()) {
            AlbumView view;
  // TODO: Reuse AlbumView objects once cover art loading is working.
//            if (convertView != null && convertView instanceof AlbumView) {
//                view = (AlbumView) convertView;
//            } else {
                view = new AlbumView(activity);
//            }
            view.setAlbum(entry, imageLoader);
            return view;

        } else {
            SongView view;
            if (convertView != null && convertView instanceof SongView) {
                view = (SongView) convertView;
            } else {
                view = new SongView(activity);
            }
            view.setSong(entry, checkable);
            return view;
        }
    }
}
